Computer Science
Paper 2 Notes
Command to print
System.out.println("<text to print>")
Declaring variables
<data type> <variable name> = <value>
Data types
- int: used for whole numbers
- double: "real" numbers (with decimals)
- String: sequence of characters
- boolean: true or false
Comparison operators
- == : equal to
- != : not equal to
- > : greater than
- < : less than
- >= : greater or equal than
- <= : less or equal than
If statements
if(<condition>){
ㅤㅤㅤㅤㅤㅤ<statement>;
};
else if(<condition>){
ㅤㅤㅤㅤㅤㅤ<statement>;
}
else{
ㅤㅤㅤㅤㅤㅤ<statement>;
};
Comparing strings
- We use the command:
.equals()
- Example:
"myName".equals("John")
Logical operators
- && : AND
- || : OR
For loop
for(<data type> <variable> = <value>; <condition>; <variable>++){
ㅤㅤㅤㅤㅤㅤ<statement>
}
Methods
- Works like a "mini program"
- Write once, use multiple times
- To call a static method, simply write its name
- You can also call methods from other classes in the same package by using the following notation: <Class name>.<method name>();
- If the data type of a method is anything except void, it must return a value
Object orientation
- A class is a blueprint which describes what something is but isn't it.
- All classes define its objects' behaviors and attributes (i.e. variables and methods)
- Every class is a data type as you can create objects from that class
How to create an object from a class
class> <object name> = new <class>();
Setters/mutators
- A method that is used to change or define a value of an attribute
- Uses parameters/arguments
How to create a setter/mutator
public void <method name>(<data type> <parameter variable>){
ㅤㅤㅤㅤㅤㅤthis.<attribute> = <parameter variable>;
}
Getters/accessor methods
- Used to access the value of a variable outside of a class
- Only way of accessing private variables
How to create a getter/accessor
public <data type of what will be returned> <method name>(){
ㅤㅤㅤㅤㅤㅤreturn this.<attribute>;
}
toString
- Method usually used to troubleshoot code
- Normally write @override before its code as toString is already a default code in Java
- It is used to print things as a string
Constructor method
- Method that creates an object from a class
- Usually public and does not have a data type
- Have the same name as the class
- Receives arguments
- A faster way to create an object with values (no need for setters)
Definitions
- Encapsulation: the technique of making the states in a class private and providing access to those attributes via public methods
- Polymorphism: methods that have the same name but different parameter lists and processes
- Instantiation: process of creating an object of a class; involves allocating memory attributes, etc.
- Variable: a name that represents a value
- Constant: a value that doesn't change when run
- Operator: allows for operations on primitive data types
- Object: Are instances of classes encapsulating attributes and methods
- Primitive: primitive data types are the predefined data types of Java (String isn't)
- Instance variable: declared in a class outside any method, an instance variable is used to store unique data for each object (are the attributes)
- Method: a subprogram that can be called by its name
- Private: can only be accessed within the declared class
- Public: can be accessed from any other class
- Modularity: having different classes and pieces of code (modules) that work independently
- Library routines: pre-written code that provides commonly used functions and procedures
- Signature: what uniquely identifies each method. It includes the parameters, parameter data type, and method identifier
- Static: a member of a class that isn't associated with an object of a class, belonging to the class itself. With this, you can access the static member without creating an object
Array
- The number of items it holds is defined when it is created
- Must consist of only one data type
- An array is an object
How to create an array
<data type>[] <array name> = new <data type>[length of array];
- e.g.
int[] ages = new int[6]
How to assign values to array
<array name> [position assigned] = <value>;
Accessing data from arrays
- Use its index number
- The first index value is 0 and the last is the (length of the array – 1)
- To access the array's length use the code: <array name>.length;
2D arrays
- Are arrays which have rows and columns
How to create a 2D array
<data type>[][] <array name> = new <data type> [number of rows] [number of columns];
The need for different data types to represent data items
- Data is stored as a combination of binary values in the computer
- Data types are needed because they specify to the computer how to interpret the binary values in the storage
Advantages of encapsulation
- Protects an object from unwanted access by clients through private variables
- Flexibility: we can change the code to read-only and write-only
- Speed of development
- Ease of maintenance: if changes are made to the data only one class needs to be changed to accommodate that. All the others will continue to use the public accessor methods and will not need to be changed
Access modifiers
- Public (+): accessible from any class
- Private (-): only accessible within the class
- Default: accessible from other classes in the same package as the enclosing class
- Protected (#): can be accessed from within the enclosing class other classes in the same package as the enclosing class sub classes regardless of package
Inheritance
- Where a sub-class has all the functionalities the superclass (methods + attributes)
Polymorphism
- Two types: overloading and overriding
- Overloading is when there are different parameters
- Overriding is when you override the method from the superclass. The original method stops doing what it was doing originally and does what it now does
How to inherit in Java
<public/private> class <subclass name> extends <superclass>
Aggregation
- When one object is composed of several others
- e.g. a Car and an engine
Association
- Both objects can exist without each other but interact with each other
Dependency
- Inheritance, association, and aggregation are types of dependencies
- It is used for temporary relations
- When one class uses another
Advantages of modularity
- Faster development: different programmers can work on different modules
- Easier to debug: it is easier to find error in smaller code (module) than larger code
- Reusability: modules can be stored and re-used for different programs
While loops
while(<condition>){
ㅤㅤㅤㅤㅤㅤ<code to be executed while the condition is true>
}
Disadvantages of using object-oriented programming (OOP)
- Not suitable for minor projects as it may lead to inefficient code
- Increases complexity for little gain for small programs
- OOP is larger than other programs thus slower and requires more memory
- Takes more effort to construct, often requiring planning
Advantages of library routines
- Saves development time since classes and their methods do not need to be re-written
- Are error-free because they have been tested multiple times
The need to reduce dependencies between objects
- Messiness: too many dependencies can make a program hard to follow and more messy
- Maintenance increase: a change to one class demands changes to other classes that depend on it
Advantages of inheritance
- Minimizes the amount of duplicate code in an application by sharing common code amongst subclasses
- Better organization of code by minimizing the amount of duplicate code
- Reusability: facility to use public methods of superclass without re-writing the same code
Advantages of polymorphism
- Overriding: allows a superclass to specify methods that will be common to all its subclasses while allowing the subclasses to define the specific implementation of some or all methods in its own specific way, suiting it to the subclass' needs
- Overloading: programmers don't need to remember and create different names for functions doing the same thing but with different parameters (e.g., SUM1, SUM2, etc.)
Features of modern programming languages that enable internationalization
- Unicode is used for the encoding and handling of text in most of today's writing systems. Modern programming languages adopt Unicode character sets allowing the computing industry to handle and represent over 65,536 characters.